package org.pr.kafkaexpress;

import lombok.extern.slf4j.Slf4j;
import org.pr.kafkaexpress.constants.KafkaConstants;
import org.pr.kafkaexpress.procedure.MessageProducer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class KafkaExpressApplication implements CommandLineRunner {

    private final MessageProducer producer;

    public KafkaExpressApplication(MessageProducer producer) {
        this.producer = producer;
    }

    public static void main(String[] args) {
        SpringApplication.run(KafkaExpressApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Starting Kafka Express application...");
        try {
            producer.send(KafkaConstants.TEST_TOPIC, KafkaConstants.DEFAULT_MESSAGE);
            log.info("Message sent successfully");
        } catch (Exception e) {
            log.error("Failed to send message", e);
        }
    }
}
